const shopService = require('../services/shopService')

const shopController = {
  async getShopByAddress(req, res, next) {
    try {
      let data = await shopService.getShopByAddress(req.query)
      data = data.filter(shop => shop.carList.find(car => car.type_id == req.query.type_id))
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getShop(req, res, next) {
    try {
      let data = await shopService.getShop()
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async addShop(req, res, next) {
    try {
      let data = await shopService.addShop(req.body)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async updateShop(req, res, next) {
    try {
      let body = req.body
      let id = body._id
      delete body.id
      let data = await shopService.updateShop(id, body)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async deleteShop(req, res, next) {
    try {
      let data = await shopService.deleteShop(req.body.id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
}

module.exports = shopController
